import 'package:city_pickers/city_pickers.dart';
import 'package:city_pickers/modal/result.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_switch/flutter_switch.dart';
import 'package:shenfuhui/config/config.dart';
import 'package:shenfuhui/views/search_appbar.dart';

import '../routers/routers.dart';
import '../views/dialog_manager.dart';

///
/// 商品分类/搜索列表
///
class ProductListPage extends StatefulWidget {
  const ProductListPage({Key? key}) : super(key: key);

  @override
  State<ProductListPage> createState() => _ProductListPageState();
}

class _ProductListPageState extends State<ProductListPage> {
  var _menuIndex = 1;

  //返回商品列表item
  getListItem(int index) {
    return InkWell(
      onTap: () {
        Routes.navigateTo(context, Routes.productDetail,
            params: {'productId': '001'});
      },
      child: Container(
        decoration: BoxDecoration(
          color: Config.colorWhite,
          borderRadius: BorderRadius.circular(10.w),
        ),
        child: Column(
          children: [
            ClipRRect(
              borderRadius: BorderRadius.circular(10.w),
              child: Image.network(
                'https://bkimg.cdn.bcebos.com/pic/dc54564e9258d109b3de9cb75d14dbbf6c81800ae728',
                fit: BoxFit.fill,
                height: 320.w,
              ),
            ),
            Padding(padding: EdgeInsets.all(5.w)),
            Container(
              padding: EdgeInsets.symmetric(horizontal: 15.w),
              width: double.infinity,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Padding(padding: EdgeInsets.only(top: 2.w)),
                  Text(
                    '商品标题',
                    style: TextStyle(color: Config.colorText1, fontSize: 28.sp),
                  ),
                  Padding(padding: EdgeInsets.only(top: 10.w)),
                  Text(
                    '商品一句话简介',
                    style: TextStyle(color: Config.colorText3, fontSize: 24.sp),
                    overflow: TextOverflow.ellipsis,
                    maxLines: 1,
                  ),
                  Padding(padding: EdgeInsets.only(top: 10.w)),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        '￥0.00',
                        style:
                            TextStyle(color: Colors.redAccent, fontSize: 24.sp),
                      ),
                      Text(
                        '库存1',
                        style: TextStyle(
                            color: Config.colorText3, fontSize: 24.sp),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Config.colorBg2,
      appBar: AppBar(
        title: SearchAppBar(
          hintLabel: '请输入商品名称',
          callBack: () {},
        ),
        centerTitle: true,
      ),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        child: Stack(
          children: [
            Container(
              height: 80.w,
              width: double.infinity,
              decoration: BoxDecoration(color: Config.colorWhite),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  InkWell(
                    onTap: () {
                      setState(() {
                        _menuIndex = 1;
                      });
                    },
                    child: Text(
                      '综合',
                      style: TextStyle(
                          fontSize: 25.sp,
                          color: _menuIndex == 1
                              ? Config.colorMain
                              : Config.colorText1),
                    ),
                  ),
                  InkWell(
                    onTap: () {
                      setState(() {
                        _menuIndex = 2;
                      });
                    },
                    child: Text(
                      '销量',
                      style: TextStyle(
                          fontSize: 25.sp,
                          color: _menuIndex == 2
                              ? Config.colorMain
                              : Config.colorText1),
                    ),
                  ),
                  Row(
                    children: [
                      InkWell(
                        onTap: () {
                          setState(() {
                            _menuIndex = 3;
                          });
                        },
                        child: Text(
                          '价格',
                          style: TextStyle(
                              fontSize: 25.sp,
                              color: _menuIndex == 3
                                  ? Config.colorMain
                                  : Config.colorText1),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
            Container(
              height: double.infinity,
              width: double.infinity,
              margin: EdgeInsets.only(top: 80.w),
              child: EasyRefresh(
                header: const MaterialHeader(),
                footer: const MaterialFooter(),
                onRefresh: () async {
                  await Future.delayed(const Duration(seconds: 1), () {
                    print('refresh');
                  });
                },
                onLoad: () async {
                  await Future.delayed(const Duration(seconds: 1), () {
                    print('load');
                  });
                },
                child: GridView.builder(
                  padding:
                      EdgeInsets.symmetric(horizontal: 25.w, vertical: 20.w),
                  itemCount: 10,
                  // shrinkWrap: true,
                  // scrollDirection: Axis.vertical,
                  // physics: const BouncingScrollPhysics(),
                  itemBuilder: (BuildContext context, int index) {
                    return getListItem(index);
                  },
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 2,
                      mainAxisSpacing: 19.w,
                      crossAxisSpacing: 19.w,
                      childAspectRatio: 0.72),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
